/**
 * Erzeugt Frame
 * @author admin
 */
package temprom.GUI;

import DataBase.DBConnection;
import DataBase.DBQuery;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JToggleButton;

/**
 *
 * @author admin
 */
public class FrmStart extends javax.swing.JPanel {

    /**
     *
     */
    public static JToggleButton b1;
//private int[] data = {
//        22, 15, 19, 04, 87, 89, 75, 88, 55, 78,
//        61, 55, 48, 60, 49, 36, 38, 27, 20, 19
//    };
    int[] data = {
        0
    };

    /**
     * Creates new form FrmStart
     */
    public FrmStart() {
        super();
        initComponents();
        b1 = btnOnOff;
        lblRealTemp.setText(Integer.toString(data[data.length - 1]));
        add(b1);
        Object[] ar = {""};
        ar = DBQuery.GetAvailableLocations(DBConnection.CreateConnection()).toArray();
        if (ar != null) {
            for (int i = 0; i < ar.length; i++) {
                locSelectPort.addItem(ar[i]);
            }
        }
    }
    /**
     *
     * @param parameters
     */
    public void SetParams(ArrayList<String> parameters)
    {
        frmChart1.SetParameters(parameters);
    }
    /**
     *
     * @return
     * Uebernimmt ComPort
     */
    public String GetComPort()
    {
        return ddSelectedPort.getSelectedItem().toString();
    }

    /**
     *
     * @throws SQLException
     */
    public void SetData() throws SQLException {

        int[] drawingData = new int[data.length + 1];
        for (int i = 0; i < drawingData.length; i++) {
            if (i < data.length) {
                drawingData[i] = data[i];
            } else {
                drawingData[i] = DBQuery.GetNewestTemperture(DBConnection.CreateConnection(), 1);
            }
        }
        data = new int[drawingData.length];
        this.data = drawingData;

        frmChart1.data = data;
        
        lblRealTemp.setText(Double.toString(frmChart1.CalculateTemperture(data[data.length - 1])).substring(0, 4));
        lblRealWerte.setText(Double.toString(data[data.length - 1]).substring(0,5));
        repaint();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitle = new javax.swing.JLabel();
        btnOnOff = new javax.swing.JToggleButton();
        ddSelectedPort = new javax.swing.JComboBox();
        locSelectPort = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        pnlRealt = new javax.swing.JPanel();
        Realpnl = new javax.swing.JScrollPane();
        lblRealTemp = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        frmChart1 = new temprom.GUI.FrmChart();
        lblRealWerte = new javax.swing.JLabel();

        setName("pnlMain"); // NOI18N

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblTitle.setText("Live Ansicht");

        btnOnOff.setText("On / Off");

        ddSelectedPort.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2", "COM3", " " }));
        ddSelectedPort.setToolTipText("");
        ddSelectedPort.setName("ddSelectedPort"); // NOI18N
        ddSelectedPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ddSelectedPortActionPerformed(evt);
            }
        });

        locSelectPort.setToolTipText("");

        jLabel1.setText("Realtime");

        lblRealTemp.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        lblRealTemp.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Realpnl.setViewportView(lblRealTemp);

        javax.swing.GroupLayout pnlRealtLayout = new javax.swing.GroupLayout(pnlRealt);
        pnlRealt.setLayout(pnlRealtLayout);
        pnlRealtLayout.setHorizontalGroup(
            pnlRealtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(pnlRealtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(Realpnl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
        );
        pnlRealtLayout.setVerticalGroup(
            pnlRealtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 97, Short.MAX_VALUE)
            .addGroup(pnlRealtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(Realpnl, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
        );

        jLabel2.setText("Temp Chart");

        frmChart1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout frmChart1Layout = new javax.swing.GroupLayout(frmChart1);
        frmChart1.setLayout(frmChart1Layout);
        frmChart1Layout.setHorizontalGroup(
            frmChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 352, Short.MAX_VALUE)
        );
        frmChart1Layout.setVerticalGroup(
            frmChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(477, 477, 477))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(ddSelectedPort, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(frmChart1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnOnOff, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(pnlRealt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(locSelectPort, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblRealWerte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ddSelectedPort, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(locSelectPort, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(btnOnOff, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlRealt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(frmChart1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblRealWerte, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ddSelectedPortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ddSelectedPortActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ddSelectedPortActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane Realpnl;
    private javax.swing.JToggleButton btnOnOff;
    private javax.swing.JComboBox ddSelectedPort;
    private temprom.GUI.FrmChart frmChart1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblRealTemp;
    private javax.swing.JLabel lblRealWerte;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JComboBox locSelectPort;
    private javax.swing.JPanel pnlRealt;
    // End of variables declaration//GEN-END:variables
}
